package com.ultrasoft.farmasoft.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.ultrasoft.farmasoft.domain.Access;
import com.ultrasoft.farmasoft.domain.Option;
import com.ultrasoft.farmasoft.domain.Submenu;
import com.ultrasoft.farmasoft.domain.Userpharmacy;


public class UtilUseFulTree {
	private List<String> listSubMenu;
	private List<Submenu> objListSubMenu;	
	private List<String> listOption;
	private List<Option> objListOption;
	
	public UtilUseFulTree() {
        listSubMenu	=new ArrayList<String>();
		listOption	=new ArrayList<String>();
		objListSubMenu	=new ArrayList<Submenu>();
		objListOption	=new ArrayList<Option>();
    }
	
	public List<String> getListSubMenu() {
		return listSubMenu;
	}
	
	public List<Submenu> getObjListSubMenu() {
		return objListSubMenu;
	}

	public void setObjListSubMenu(List<Submenu> objListSubMenu) {
		this.objListSubMenu = objListSubMenu;
	}

	public List<Option> getObjListOption() {
		return objListOption;
	}

	public void setObjListOption(List<Option> objListOption) {
		this.objListOption = objListOption;
	}

	public void setListSubMenu(List<String> listSubMenu) {
		this.listSubMenu = listSubMenu;
	}
	
	public List<String> getListOption() {
		return listOption;
	}
	
	public void setListOption(List<String> listOption) {
		this.listOption = listOption;
	}	
		
	public void converterToListOfArray(HttpServletRequest request){
		try {
			String[] arraySubmenu= request.getParameterValues("check_submenu");
			String[] arrayOption = request.getParameterValues("check_option");
			converterToListOfArray(arraySubmenu,arrayOption);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void converterToListOfArrays(List<Access> list,Userpharmacy userPharmacy){
		List<String> listTemp=new ArrayList<String>();
		for (Access access : list) {
			if(access.getAccessPK().getPharmacyId() == userPharmacy.getUserpharmacyPK().getPharmacyId()
					&& access.getAccessPK().getUserId() == userPharmacy.getUserpharmacyPK().getUserId()){
				String s=access.getAccessPK().getMenuId()+"_"+access.getAccessPK().getSubmenuId()+"_"+access.getAccessPK().getOptionId();
				listTemp.add(s);
			}
		}
		String[] array= new String[listTemp.size()];
		for (int i = 0; i < array.length; i++) {
			array[i]=listTemp.get(i);
		}		
		converterToListOfArray(null,array);
	}
	
	private void converterToListOfArray(String[] arraySubmenu,String[] arrayOption){
		if(arraySubmenu != null && arrayOption != null){
			System.out.println("Ambos son difenetes de nullos");			
			converterToListOfArrayforSubmenu(arraySubmenu);
			converterToListOfArrayforOption(arrayOption);
			obtainArrayStringOfStringOption(arrayOption);		
			removeCommonElementOfSubmenu();
			linkSubMenuToOption();
			/*for (Iterator iterator = objListSubMenu.iterator(); iterator.hasNext();) {
				Submenu type = (Submenu) iterator.next();
				System.out.println("tam de coleccion: "+type.getOptionCollection().size());
			}*/
		}else{
			if(arraySubmenu == null && arrayOption != null){
				System.out.println("Es nulo check_submenu y tiene elementos el check_option....");
				converterToListOfArrayforOption(arrayOption);
				obtainArrayStringOfStringOption(arrayOption);
				removeCommonElementOfSubmenu();
				linkSubMenuToOption();
				/*for (Iterator iterator = objListSubMenu.iterator(); iterator.hasNext();) {
					Submenu type = (Submenu) iterator.next();
					System.out.println("tam de coleccion: "+type.getOptionCollection().size());
				}*/
			}else
				if(arraySubmenu != null && arrayOption == null){
					System.out.println("Es nulo check_option y tiene elementos el check_submenu....");
					converterToListOfArrayforSubmenu(arraySubmenu);
					removeCommonElementOfSubmenu();
					linkSubMenuToOption();
					/*for (Iterator iterator = objListSubMenu.iterator(); iterator.hasNext();) {
						Submenu type = (Submenu) iterator.next();
						System.out.println("tam de coleccion: "+type.getOptionCollection().size());
					}*/
				}
		}
	}
	
	private void linkSubMenuToOption(){
		for (Submenu sm : objListSubMenu) {
			List<Option> tempOption=new ArrayList<Option>();
			for (Option o : objListOption) {	
				if(o.getOptionPK().getMenuId() == sm.getSubmenuPK().getMenuId() && o.getOptionPK().getSubmenuId()==sm.getSubmenuPK().getSubmenuId())
					tempOption.add(o);
			}
			sm.setOptionCollection(tempOption);
		}
	}
	
	private void obtainArrayStringOfStringOption(String[] arrayOption){
		for (int j = 0; j < arrayOption.length; j++) {
			String[] id = arrayOption[j].split("_");
			listSubMenu.add(id[0]+"_"+id[1]);
			objListSubMenu.add(new Submenu(Short.parseShort(id[0]),Short.parseShort(id[1])));
		}
	}
	
	private boolean search(List<String> list,String s){
		boolean temp=false;
		for (String string : list) {
			if(s.equals(string))
				temp=true;
		}
		return temp;
	}
	
	private void removeCommonElementOfSubmenu(){
		List<String> list=new ArrayList<String>();
		List<Submenu> listSubmenuTemp=new ArrayList<Submenu>();
		for (String string : listSubMenu) {	
			if(!search(list,string)){
				list.add(string);
				String[] id = string.split("_");
				listSubmenuTemp.add(new Submenu(Short.parseShort(id[0]),Short.parseShort(id[1])));
			}
		}
		objListSubMenu=new ArrayList<Submenu>();
		listSubMenu=new ArrayList<String>();
		listSubMenu.addAll(list);
		objListSubMenu.addAll(listSubmenuTemp);
	}
	
	private void converterToListOfArrayforSubmenu(String[] arraySubmenu){
    	try {
    		for (int o = 0; o < arraySubmenu.length; o++) {
    			listSubMenu.add(arraySubmenu[o]);
    			String[] id = arraySubmenu[o].split("_");
    			objListSubMenu.add(new Submenu(Short.parseShort(id[0]),Short.parseShort(id[1])));
    		}
		} catch (Exception e) {}
    }
    
    private void converterToListOfArrayforOption(String[] arrayOption){
    	try {
    		for (int o = 0; o < arrayOption.length; o++) {
        		listOption.add(arrayOption[o]);
    			String[] id = arrayOption[o].split("_");
    			objListOption.add(new Option(Short.parseShort(id[0]),Short.parseShort(id[1]),Short.parseShort(id[2])));
        	}
		} catch (Exception e) {}
    }
}
